<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: bilder.php 6 2005-07-11 23:14:47Z say $
 */
 
include_once("db.php");
include_once("laivid.php");
include_once("design.php");

$arr=getLaiv();
$arrid=$arr['ID'];
$sidetittel=$arr['Tittel']." - Bilder";
include("top.php");

if (time() > strtotime($arr['Roller_release'])) {
	$mode=$_REQUEST['mode'];

	if ($mode == 'one') {
		$num = $_REQUEST['num'];
		displayImage($num);
	} elseif ($mode == 'thumb') {
		$startnum=$_REQUEST['startnum'];

		if (!is_numeric($startnum))
			$startnum=0;

		displayThumbs($startnum);
	} else {
		$startnum=0;
		displayThumbs($startnum);
	}
} else {
	text("Not ready yet!");
}


include("bottom.php");

function displayImage($num) {
	global $arrid;
	$sql = "SELECT ID, Bilde_w, Bilde_h, Beskrivelse FROM Bilder WHERE LaivID=$arrid LIMIT $num, 1";
	$bilder=mysql_query($sql);

	if (mysql_num_rows($bilder)) {
		$bildet=mysql_fetch_assoc($bilder);

		$displaynum=$num+1;

		headline("Bilde $displaynum");
		$uttxt="<p>{$bildet['Beskrivelse']}</p>";
		$uttxt.="<table><tr><td colspan=\"3\">";
		$uttxt.="<img src=\"bilde_vis.php?id={$bildet['ID']}\" width=\"{$bildet['Bilde_w']}\" height=\"{$bildet['Bilde_h']}\" alt=\"{$bildet['Beskrivelse']}\"/>";
		$uttxt.="</td></tr><tr>";

		if ($num > 0) {
			$backnum=$num - 1;
			$uttxt.="<td style=\"width:33%\"><a href=\"bilder.php?mode=one&num=$backnum\">[tilbake]</a></td>";
		} else
			$uttxt.="<td style=\"width:33%\"></td>";

		$oppnum=((int)($num/10))*10;
		$uttxt.="<td style=\"width:34%; text-align:center\"><a href=\"bilder.php?mode=thumb&startnum=$oppnum\">[opp]</a></td>";
		$sql = "SELECT ID FROM Bilder WHERE LaivID=$arrid";
		$recs=mysql_query($sql);
		$numbilder=mysql_num_rows($recs);

		if ($num < $numbilder-1) {
			$nextnum=$num+1;
			$uttxt.="<td style=\"width:33%; text-align:right\"><a href=\"bilder.php?mode=one&num=$nextnum\">[neste]</td>";
		} else
			$uttxt.="<td style=\"width:33%;\"></td>";
		$uttxt.="</tr></table>";
		text($uttxt);
	}
}

function displayThumbs($startnum) {
	global $arrid;
	$sql = "SELECT ID, Thumb_w, Thumb_h, Beskrivelse FROM Bilder WHERE LaivID=$arrid LIMIT $startnum, 10";

	$bilder=mysql_query($sql) or die(mysql_error());

	$sql = "SELECT ID FROM Bilder WHERE LaivID=$arrid";
	$recs=mysql_query($sql);
	$numbilder=mysql_num_rows($recs);

	$i=0;

	$displaystart=$startnum+1;
	$displayslutt=$startnum+10;

	if ($displayslutt > $numbilder)
		$displayslutt=$numbilder;
	$uttxt="<p>Viser bilde $displaystart-$displayslutt av totalt $numbilder for denne laiven.</p>";

	$curpos=0;
	$numlinks="";
	while ($curpos < $numbilder) {
		$disp_cur=$curpos+1;
		$disp_curend=$curpos+10;
		if ($disp_curend > $numbilder)
			$disp_curend=$numbilder;

		if ($curpos == $startnum)
			$numlinks.= "[$disp_cur-$disp_curend] ";
		else
			$numlinks.="<a href=\"bilder.php?mode=thumb&startnum=$curpos\">[$disp_cur-$disp_curend]</a>";
		$curpos += 10;
	}
	$uttxt.="<p>$numlinks</p>";
	$uttxt.="<table>\n";
	while($bilde=mysql_fetch_assoc($bilder)) {
		$nummet=$startnum+$i;
		if (! ($i % 2))
			$uttxt.="<tr>\n";
			
		$uttxt.="<td><a href=\"bilder.php?mode=one&num=$nummet\"><img src=\"bilde_vis.php?id={$bilde['ID']}&thumb=true\"></a></td>\n";
		$uttxt.="<td><a href=\"bilder.php?mode=one&num=$nummet\">{$bilde['Beskrivelse']}</a></td>";
		
		if($i % 2)
			$uttxt.="</tr>\n";
		$i++;
	}
	$uttxt.="</table>\n";

	text($uttxt, false);
}
	
?>
